package com.woniu.crowd.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.crowd.client.CrowdQueryClient;
import com.woniu.crowd.client.param.SearchCrowdParam;
import com.woniu.crowd.client.result.CrowdProductResultInfo;
import com.woniu.crowd.service.CrowdfundingProductsService;
import com.woniu.crowd.service.dto.CrowdProductDto;
import com.woniu.crowd.service.form.SearchCrowdForm;
import com.woniu.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class CrowdQueryApiImpl implements CrowdQueryClient {
    @Resource
    private CrowdfundingProductsService crowdfundingProductsService;

    @Override
    public Result searchCrowd(SearchCrowdParam searchCrowd) {
        log.info("搜索众筹商品:{}", searchCrowd);
        SearchCrowdForm searchCrowdForm = BeanUtil.copyProperties(searchCrowd, SearchCrowdForm.class);
        return crowdfundingProductsService.searchCrowd(searchCrowdForm);
    }

    @Override
    public Result<List<CrowdProductResultInfo>> listCrowdByUserId(Integer userId) {
        log.info("用户查询个人参与的众筹:{}", userId);
        List<CrowdProductDto> crowdProductDtos = crowdfundingProductsService.listCrowdByUserId(userId);
        List<CrowdProductResultInfo> crowdProductResultInfos = BeanUtil.copyToList(crowdProductDtos, CrowdProductResultInfo.class);
        return Result.getInstanceList(CrowdProductResultInfo.class).setMsg("查询成功").setCode(200).setData(crowdProductResultInfos);
    }
}
